﻿Imports Telerik.Windows.Controls
Imports Telerik.Windows.Data
Imports Global_Visas_Business_Layer
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text
Partial Public Class frmAdminCountries

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        Me.Close()
    End Sub


    Private Sub frmAdminCountries_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        refreshList()

        'Dim datatable As DataTable = Global_Visas_Business_Layer.CommonDataFunctions.GetSQLDatatable("select * from l_county")

        'Dim sqlconn As New SqlConnection("Server=10.0.0.101;Database=global;User Id=GlobalWeb;Password=0cSn3FKzr9;")
        'Dim sqlCommand As New SqlCommand()
        'With sqlCommand
        '    .Connection = sqlconn
        '    .CommandType = CommandType.Text
        '    .CommandText = "insert into l_county values(@param)"
        '    .Parameters.Add("@param", SqlDbType.NVarChar)
        'End With

        'For Each currentRow As DataRow In datatable.Rows
        '    sqlconn.Open()
        '    sqlCommand.Parameters(0).Value = currentRow(1).ToString()
        '    sqlCommand.ExecuteNonQuery()
        '    sqlconn.Close()
        'Next

    End Sub

    Private Sub refreshList()
        Me.grdCountries.ItemsSource = Admin.CountryList
        Me.grdCountries.Columns(0).IsVisible = False
        ' Me.grdQualifications.Columns(3).IsVisible = False
    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
        ClearControls()
        Me.btnSave.IsEnabled = True
        Me.btnCancel.IsEnabled = True
        txtCountry.Focus()
    End Sub
    Private Sub ClearControls()
        Me.txtCountry.Text = String.Empty
        Me.txtNationality.Text = String.Empty
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
        If txtCountry.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Please enter Country !", CommonDialog.CustomDialogIcons.Information, "Error")
            txtCountry.Focus()
            Exit Sub
        ElseIf txtNationality.Text.Trim = String.Empty Then
            CommonFuncs.DisplayMessage("Please select Nationality !", CommonDialog.CustomDialogIcons.Information, "Error")
            txtNationality.Focus()
            Exit Sub
        Else
            Dim firstMsgboxResult, secondMsgboxResult As MessageBoxResult
            Dim intEU, intMigration As Integer
            intEU = 0
            intMigration = 0
            firstMsgboxResult = MessageBox.Show("Is this a EU country? (i.e. no visa required to stay in UK)", "", MessageBoxButton.YesNo, MessageBoxImage.None)
            If firstMsgboxResult = MessageBoxResult.Yes Then
                intEU = 1
            ElseIf firstMsgboxResult = MessageBoxResult.No Then
                intEU = 0
            End If
            secondMsgboxResult = MessageBox.Show("Do we handle migration to this country ?", "", MessageBoxButton.YesNo, MessageBoxImage.None)
            If secondMsgboxResult = MessageBoxResult.Yes Then
                intMigration = 1
            ElseIf secondMsgboxResult = MessageBoxResult.No Then
                intMigration = 0
            End If
            Dim Sql As New StringBuilder
            With Sql
                .Append("")
                .Append("insert into l_country ")
                .Append("(Country_Name,Country_Nationality,Country_EU,Country_Migration)")
                .Append(" Values(")
                .Append(" '" & Me.txtCountry.Text & "',")
                .Append(" '" & Me.txtNationality.Text & "',")
                .Append(intEU & ",")
                .Append(intMigration & ")")
            End With

            If CommonDataFunctions.ExecuteSQL(Sql.ToString) = True Then
                refreshList()
                CommonFuncs.DisplayMessage("The new Country has been added successfully.", CommonDialog.CustomDialogIcons.Information, "Success")
                'Exit Sub
            Else
                CommonFuncs.DisplayMessage("There is an error adding the Country to the system. Please try again or contact IT", CommonDialog.CustomDialogIcons.Information, "Unsuccessful")
                'Exit Sub
            End If
            Me.btnAddNew.IsEnabled = True
            Me.btnCancel.IsEnabled = False
            Me.btnSave.IsEnabled = False

        End If

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnCancel.Click
        Me.btnAddNew.IsEnabled = True
        Me.btnSave.IsEnabled = False
        Me.btnCancel.IsEnabled = False
    End Sub
End Class
